package mybatis.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import mybatis.dao.UserDao;
import mybatis.entity.User;
import mybatis.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author：汤小洋
 * Date：2023-08-11 17:04
 * Description：<描述>
 */
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    private  final UserDao userDao;

    @SneakyThrows
    // @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT,rollbackFor = Exception.class)
    @Override
    public void register(User user) {
        userDao.insert(user);

        // System.out.println(5/0);
        new SimpleDateFormat("yyyy-MM-dd").parse("123123");
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true, timeout = 5)
    @Override
    public User login(String username, String password) {
        return userDao.selectByUsernameAndPassword(username,password);
    }

    @Override
    public List<User> findAll() {
        return userDao.selectAll();
    }

    @Override
    public PageInfo<User> findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        List<User> users = userDao.selectAll();

        PageInfo<User> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }
}
